require 'hpricot'

module TalkShoe
  class EpisodeList
    include Enumerable
    
    attr_reader :episodes
    
    def initialize(options)
      if options.is_a?(String)
        initialize_from_xml(options) 
      elsif options.is_a?(Array)
        initialize_from_array(options)
      else
        raise ArgumentError.new("Cannot create TalkShoe::EpisodeList from #{options}")
      end
    end
    
    def initialize_from_xml(xml)
      doc = Hpricot.XML(xml)
      @episodes = []
      (doc/'return').each do |ep|
        self.episodes << Episode.new(ep.to_s)
      end
    end
    
    def initialize_from_array(eps)
      @episodes = []
      eps.each do |e|
        if e.kind_of(TalkShoe::Episode)
          episodes << e
        else
          episodes << TalkShoe::Episode.new(e)
        end
      end
    end
    
    def each( &block )
      episodes.each(&block)
    end
    
    def to_html
      episodes.inject("") { |string, ep| string += (ep.to_html + '<br/>')}
    end
    
    private :initialize_from_xml, :initialize_from_array
  end
end